﻿/* 功能:  商户收款管理
 * 创建人：Kencery  创建时间：2018-2-3
 */
var ml = window.ml || {};
ml.admin = ml.admin || {};

ml.admin.receivables = (function () {
    var defaults = {
        listUrl: "/Receivables/List",
        addUrl: "/Receivables/Add",
        editUrl: "/Receivables/Edit",
        setDefaultMarkUrl: "/Receivables/SetDefaultMark",
        delUrl: "/Receivables/Delete",
        MerchantId: "",
    };
    var opt = {};

    var columns = [{
        field: 'state',
        radio: true
    }, {
        title: '行号',
        field: 'number',
        align: 'center',
        hide: true,
        formatter: plscommon.tableNumber
    }, {
        title: 'ID',
        field: 'Id',
        align: 'center',
    }, {
        title: '收款方式',
        field: 'Method'
    }, {
        title: '开户行',
        field: 'OpeningBank',
        align: 'center',
    }, {
        title: '开户名',
        field: 'OpeningName',
        align: 'center',
    }, {
        title: '开户帐号',
        field: 'OpeningAccount',
        align: 'center',
    }, {
        title: '三方帐号',
        field: 'ThirdAccount',
        align: 'center',
    }, {
        title: '默认',
        field: 'DefaultMark',
        align: 'center',
        formatter: function (value, rows, index) {
            if (value == 1) {
                return "默认";
            }
            if (value == 0) {
                return "非默认";
            }
        }
    }, {
        title: '创建时间',
        field: 'CreateTime',
        align: 'center',
    }];

    var initTable = function () {
        plscommon.bootstraptable({
            id: "#receivablesinfo",
            url: defaults.listUrl,
            queryParams: queryParams,
            uniqueId: "Id",
            columns: columns
        });
    };

    var queryParams = function (params) {
        return {
            offset: params.offset,                  //后台计算显示数据信息
            pagesize: params.limit,                 //每页显示多少行
            Method_Search: $("#method_search").val()
        };
    };

    var clickEvent = function () {
        $("#btnQueryList").on("click", function () { btnQueryList(); });                            //按条件查询结果
        $("#btnReset").on("click", function () { btnReset(); });                                    //清空文本框信息

        $("#btnAddDialog").on("click", function () { btnAddDialog() });                             //添加弹出浮层
        $("#btnEditDialog").on("click", function () { btnEditDialog() });                           //修改弹出浮层
        $("#btnAddEditOK").on("click", function () { btnAddEditOK(); });                            //添加修改方法

        $("#btnYesSetDefaultDialog").on("click", function () { btnYesSetDefaultDialog(); });        //设置默认收款弹出层
        $("#btnNoSetDefaultDialog").on("click", function () { btnNoSetDefaultDialog(); });          //设置非默认收款弹出层
        $("#btnSetDefaultOK").on("click", function () { btnSetDefaultOK(); });                      //设置默认非默认确定方法

        $("#btnDelDialog").on("click", function () { btnDelDialog(); });                            //删除弹出浮层
        $("#btnDeleteOK").on("click", function () { btnDeleteOK(); });                              //删除方法 
    };

    var btnQueryList = function () {
        plscommon.refreshTable("receivablesinfo");
    }

    var btnReset = function () {
        plscommon.resetFrom("formSearch");
    }

    var btnAddDialog = function () {
        if (defaults.MerchantId == "") {
            plscommon.warningMessage("该商户还未配置店铺信息，不允许添加", 4000);
            return false;
        }

        $('#AddEditModal').modal();
        plscommon.resetFrom("AddEditOperation");
        $("#HeadTitle").text("添加收款方式");
        opt.type = 1;
    }

    var btnEditDialog = function () {
        var data = plscommon.dialogWaring('receivablesinfo', 'AddEditModal', 'AddEditOperation');
        if (!data.Id) {
            return false;
        }

        $('#AddEditModal').modal();
        plscommon.resetFrom("AddEditOperation");
        $("#HeadTitle").text("修改收款方式");
        opt.type = 2;

        //赋值给控件信息
        $("#Id").val(data.Id);
        $("#Method").val(data.Method);
        $("#OpeningBank").val(data.OpeningBank);
        $("#OpeningName").val(data.OpeningName);
        $("#OpeningAccount").val(data.OpeningAccount);
        $("#ThirdAccount").val(data.ThirdAccount);
    }

    var btnAddEditOK = function () {
        //读取form表单中的值并且进行验证
        var data = plscommon.deleteByValue($("#AddEditOperation").serializeArray(), "Id")
        data = plscommon.transitionArray(data);

        if (!checkData(data)) {
            return false;
        }

        $("#btnAddEditOK").prop("disabled", true);
        if (opt.type == 1) {
            data.Method = $("#Method").val();
            plscommon.ajax_status({
                disableId: "btnAddEditOK",
                url: defaults.addUrl,
                type: "POST",
                data: data,
                success: function () {
                    if (this.status_code === 200) {
                        $('#AddEditModal').modal('hide');
                        plscommon.refreshTable("receivablesinfo");
                    } else {
                        plscommon.warningMessage(this.status_message, 3000);
                    }
                }
            });
        } else {
            data.Id = $("#Id").val();
            plscommon.ajax_status({
                disableId: "btnAddEditOK",
                url: defaults.editUrl,
                type: "POST",
                data: data,
                success: function () {
                    if (this.status_code === 200) {
                        $('#AddEditModal').modal('hide');
                        plscommon.refreshTable("receivablesinfo");
                    } else {
                        plscommon.warningMessage(this.status_message, 3000);
                    }
                }
            });
        }
    }

    var btnYesSetDefaultDialog = function () {
        var data = plscommon.dialogWaring('receivablesinfo', 'SetDefaultMarkModal', 'SetDefaultMarkOperation');
        if (!data.Id) {
            return false;
        }

        //设置默认
        plscommon.resetFrom("SetDefaultMarkOperation");
        $("#defaultmark_id").val(data.Id);
        $("#DefaultMark_Head_Title").text(data.OpeningBank);
        $("#DefaultMark_Head_Title_OK").text("（" + data.OpeningBank + "）" + "为默认");
        opt.DefaultMark = 1;
    }

    var btnNoSetDefaultDialog = function () {
        var data = plscommon.dialogWaring('receivablesinfo', 'SetDefaultMarkModal', 'SetDefaultMarkOperation');
        if (!data.Id) {
            return false;
        }

        //设置非默认
        plscommon.resetFrom("SetDefaultMarkOperation");
        $("#defaultmark_id").val(data.Id);
        $("#DefaultMark_Head_Title").text(data.OpeningBank);
        $("#DefaultMark_Head_Title_OK").text("（" + data.OpeningBank + "）" + "为默认");
        opt.DefaultMark = 0;
    }

    var btnSetDefaultOK = function () {
        var data = plscommon.dialogWaring('receivablesinfo', 'SetDefaultMarkModal', 'SetDefaultMarkOperation');
        if (opt.DefaultMark == 0 && data.DefaultMark == 0) {  //非默认
            plscommon.warningMessage("该支付方式已经是非默认状态，不允许再设置非默认", 3000);
            return false;
        }
        if (opt.DefaultMark == 1 && data.DefaultMark == 1) {  //非默认
            plscommon.warningMessage("该支付方式已经是默认状态，不允许再设置默认", 3000);
            return false;
        }

        $("#btnSetDefaultOK").prop("disabled", true);
        plscommon.ajax_status({
            disableId: "btnSetDefaultOK",
            url: defaults.setDefaultMarkUrl,
            type: "POST",
            data: { Id: $("#defaultmark_id").val(), DefaultMark: opt.DefaultMark },
            success: function () {
                if (this.status_code === 200) {
                    $('#SetDefaultMarkModal').modal('hide');
                    plscommon.refreshTable("receivablesinfo");
                } else {
                    plscommon.warningMessage(this.status_message, 3000);
                }
            }
        });
    }

    var btnDelDialog = function () {
        var data = plscommon.dialogWaring('receivablesinfo', 'DelModal', 'DelOperation');
        if (!data.Id) {
            return false;
        }

        $("#delete_id").val(data.Id);
        $("#Delete_Head_Title").text(data.OpeningBank);
        $("#Delete_Head_Title_OK").text(data.OpeningBank);
    }

    var btnDeleteOK = function () {
        var delete_id = $("#delete_id").val();
        if (!delete_id) {
            plscommon.warningMessage("请您选择需要删除的数据", 4000);
            return false;
        }

        $("#btnDeleteOK").prop("disabled", true);
        plscommon.ajax({
            disableId: "btnDeleteOK",
            url: defaults.delUrl,
            type: "POST",
            data: { id: delete_id },
            success: function () {
                $('#DelModal').modal('hide');
                $("#receivablesinfo").bootstrapTable('refresh');
            }
        });
    };

    var checkData = function (data) {
        if (!data.OpeningBank) {
            plscommon.warningMessage("开户行不能为空，请您检查", 3000);
            return false;
        }
        if (!data.OpeningName) {
            plscommon.warningMessage("开户名不能为空，请您检查", 3000);
            return false;
        }
        if (!data.OpeningAccount) {
            plscommon.warningMessage("开户账号不能为空，请您检查", 3000);
            return false;
        }
        return true;
    }

    return {
        init: function (options) {
            $.extend(defaults, options || {});
            initTable();            //初始化Table表格
            clickEvent();           //触发事件
        }
    };
}());